//
//  HJDCTimeViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/5/23.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDCTimeViewController.h"
#import "HJDCTimeTableViewCell.h"
#import "HJDCTimeModel.h"
#import "HJDDetailTimeTableViewController.h"
#import "HJDCTimeViewController.h"

@interface HJDCTimeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSArray * CTimeArray;

@property (nonatomic,strong) NSMutableArray * indexArray;




@end

@implementation HJDCTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"产检时间表";
     self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(shareBtnClick) image:@"矢量智能对象" HighImage:nil];
    self.indexArray = [[NSMutableArray alloc]init];
    
    [self setUpCell];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(popToPre) image:@"︿" HighImage:nil];
    
}

-(void)popToPre
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)setUpCell
{
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.CTimeArray = [HJDCTimeModel cTimeArray];
    [self.view addSubview:self.tableView];
//    [[NSUserDefaults standardUserDefaults][setValue: forKey:@"aaa"] ];
//    HJDCTimeTableViewCell *Tcell = [self.tableView cellForRowAtIndexPath:i];
//    [[NSUserDefaults standardUserDefaults]objectForKey:@"aaa"];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.CTimeArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    HJDCTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HJDCTimeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    HJDCTimeModel * model = self.CTimeArray[indexPath.row];
    cell.dateLbl.text = model.date;
    cell.numLbl.text = model.numTimes;
    cell.weekLbl.text = model.week;
    cell.stessLbl.text = model.stess;
//    防止背景颜色被覆盖，变成透明
    cell.stessLbl.backgroundColor = [UIColor clearColor];
    cell.stessLbl.layer.backgroundColor = [UIColor grayColor].CGColor;
    cell.projectLbl.text = model.project;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;

    if ([self.indexArray containsObject:indexPath]) {
        cell.dateLbl.textColor = [UIColor redColor];
        
    }else{
        cell.dateLbl.textColor = [UIColor grayColor];
    }
    
//    if (indexPath.row == self.CTimeArray[indexPath.row] ) {
//        cell.accessoryType = UITableViewCellAccessoryCheckmark;
//        NSUserDefaults * de = [NSUserDefaults standardUserDefaults];
//        [de setValue:[self.CTimeArray objectAtIndex:indexPath.row] forKey:@"aaa"];
//        NSLog(@"%@",de);
//        [[NSUserDefaults standardUserDefaults]setValue:[self.CTimeArray objectAtIndex:indexPath.row] forKey:@"aaa"];
//    }
//         [[NSUserDefaults standardUserDefaults]setValue:[self.CTimeArray objectAtIndex:indexPath.row] forKey:@"aaa"];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJDDetailTimeTableViewController *detail = [[HJDDetailTimeTableViewController alloc]init];
//    传值过去
    detail.ctimeModel = [HJDCTimeModel cTimeArray][indexPath.row];
    
    [self.navigationController pushViewController:detail animated:YES];
    
    HJDCTimeTableViewCell *Tcell = [tableView cellForRowAtIndexPath:indexPath];
    Tcell.dateLbl.textColor = [UIColor getColorWithHexNumber:@"FF8395"];
    [Tcell.btnImage setImage:[UIImage imageNamed:@"矢量智能对象@2x_25"] forState:UIControlStateNormal];
    Tcell.stessLbl.backgroundColor = [UIColor getColorWithHexNumber:@"FF8395"];
    Tcell.projectLbl.textColor = [UIColor getColorWithHexNumber:@"BBBBBB"];
    [self.indexArray addObject:indexPath];
   

   }

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJDCTimeTableViewCell *TCell = [tableView cellForRowAtIndexPath:indexPath];
    TCell.textLabel.textColor = [UIColor grayColor];
    if ([self.indexArray containsObject:indexPath]) {
        [self.indexArray removeObject:indexPath];
    }
}

-(void)shareBtnClick
{
    NSLog(@"分享");
}

@end
